//实现了用http模块做简单的前后端数据交互
//服务器处理表单提交 post和get请求，并对其参数做出相应的响应
const http = require("http");
const fs = require("fs");
let user = { username: "admin1", password: "12345" };
const server = http.createServer((req, res) => {
    res.writeHead(200, { "Content-Type": "text/html;charset=utf-8" });
    let data = "";
    let params=req.url.split("?")[1];
    req.url=req.url.split('?')[0]
    switch (req.url) {
        case "/":
        case "/index":
            data = fs.readFileSync(__dirname + "/index.html", "utf8");
            res.end(data);
            break;
        case "/login":
            data = fs.readFileSync(__dirname + "/login.html", "utf8");
            fs.readFile(__dirname + "/persons.js", "utf8", function (err, data) {
                let per = JSON.parse(data);
                per.find(v => v.username == user.username && v.password == user.password) ? console.log("登陆成功") : console.log("用户名或密码错误");
            });
            res.end(data);
            break;
        case "/reg":
        case "/register":
            data = fs.readFileSync(__dirname + "/register.html", "utf8");
            // reg()
            res.end(data);
            break;
        case "/post":
            let postParams = "";
            req.on("data", params => {
                postParams += params;
            });
            console.log(postParams);
                req.on("end", () => {
                    if(postParams) {
                        user = {
                            username: postParams.split("&")[0].split("=")[1],
                            password: postParams.split("&")[1].split("=")[1],
                        };
                        console.log(user);
                        let fd = fs.readFileSync(__dirname + "/persons.js", "utf8");
                        let per = JSON.parse(fd);
                        console.log(fd);
                        let tag = per.find(v => v.username == user.username);
                        if (tag) {
                            console.log("已有用户");
                            res.end(`<h1>已有账号，3秒后跳转回注册页</h1>
                            <script>
                                setTimeout(() => {
                                    location.href='http://127.0.0.1/reg'
                                }, 3000);
                            </script>`);
                        } else {
                            console.log("注册成功");
                            res.end(`<h1>注册成功，3秒后跳转登录页</h1>
                            <script>
                                setTimeout(() => {
                                    location.href='http://127.0.0.1/login'
                                }, 3000);
                            </script>`);
                            per.push({
                                username: user.username,
                                password: user.password,
                            });
                            fs.writeFile(__dirname + "/persons.js", JSON.stringify(per), function (err) {
                                if (err) {
                                    console.log(err);
                                } else {
                                    console.log("追加成功");
                                }
                            });
                        }
                    }else {
                        data = fs.readFileSync(__dirname + "/loading.html", "utf8");
                        res.end(data);
                    }
                });
            break;
        case "/get":
            if (params) {
                console.log(params);
                user = {
                    username: params.split("&")[0].split("=")[1],
                    password: params.split("&")[1].split("=")[1],
                };
                fs.readFile(__dirname + "/persons.js", "utf8", function (err, data) {
                    let per = JSON.parse(data);
                    per.find(v => v.username == user.username && v.password == user.password) ? res.end(`<h1>登陆成功，3秒后跳转到首页</h1>
                    <script>
                        setTimeout(() => {
                            location.href='http://127.0.0.1/index'
                        }, 3000);
                    </script>`) : res.end(`<h1>用户名或密码错误，3秒后跳转回登录页</h1>
                    <script>
                        setTimeout(() => {
                            location.href='http://127.0.0.1/login'
                        }, 3000);
                    </script>`);
                });
            }else {
                data = fs.readFileSync(__dirname + "/loading.html", "utf8");
                res.end(data);
            }
            break;
        default:
            data = fs.readFileSync(__dirname + "/loading.html", "utf8");
            res.end(data);
            break;
    }
});
server.listen(80, () => {
    console.log("服务已开启");
});
